package com.sgb.jicai.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.jicai.BR;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ItemFactoryParticipationShoppingListBinding;
import com.sgb.link.view.widget.EditJcNumPopWindow;
import com.swgk.core.util.TimeUtil;
import com.sgb.jicai.entity.FactoryParticipationEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 供方发起-立即参与页面--采购清单列表
 */
public class FactoryParticiShoppingListAdapter extends BaseRecycleAdapter2<FactoryParticipationEntity> {

    private Context context;
    private Calendar selectedDate = Calendar.getInstance();
    private Calendar dayCalendar = Calendar.getInstance();

    public FactoryParticiShoppingListAdapter(Context context, List<FactoryParticipationEntity> datas) {
        super(context, R.layout.item_factory_participation_shopping_list, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, FactoryParticipationEntity entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        View llAttrs = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).llAttrs;
        if (!TextUtils.isEmpty(entity.getAttrs())){
            llAttrs.setVisibility(View.VISIBLE);
        }else{
            llAttrs.setVisibility(View.GONE);
        }
        updateRecycler(((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).childRecycler, entity);
        TextView tvTime = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).tvTime;
        ImageView ivSub = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).ivSub;
        TextView tvNum = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).tvNum;
        ImageView ivAdd = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).ivAdd;
        View viewBottom = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).viewBottom;
        LinearLayout llOriginalPrice = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).llOriginalPrice;//原价布局
        TextView tvOriginalPrice = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).tvOriginalPrice;//原价展示
        TextView rulingPrice = ((ItemFactoryParticipationShoppingListBinding) holder.getmBinding()).rulingPrice;//现价
        if (!TextUtils.isEmpty(entity.getOriginalPrice())){
            llOriginalPrice.setVisibility(View.VISIBLE);
            tvOriginalPrice.setText("原价:"+entity.getOriginalPrice());
            rulingPrice.setText("现价:");
        }else {
            llOriginalPrice.setVisibility(View.GONE);
            rulingPrice.setText("集采价格:");
        }
        //小数点后保留两位数字
        ivSub.setOnClickListener(v -> sub(tvNum, entity));
        tvNum.setOnClickListener(v -> num(tvNum, entity));
        ivAdd.setOnClickListener(v -> add(tvNum, entity));
        tvTime.setOnClickListener(v -> selectTime(tvTime, entity));
        if (getDatas().size() - 1 == position) {
            viewBottom.setVisibility(View.GONE);
        } else {
            viewBottom.setVisibility(View.VISIBLE);
        }
    }

    //更新子列表显示
    private void updateRecycler(RecyclerView recyclerView, FactoryParticipationEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        FactoryParticiShoppingChildListAdapter childAdapter = new FactoryParticiShoppingChildListAdapter(recyclerView.getContext(), entity.getPriceRules(), entity.getUnitName());
        recyclerView.setAdapter(childAdapter);
    }

    private void sub(TextView tvNum, FactoryParticipationEntity entity) {
        double num = Double.parseDouble(tvNum.getText().toString());
        if (num < 1) {
            num = 0;
        } else {
            num--;
        }
        DecimalFormat df = new DecimalFormat("#####0.00");
        tvNum.setText(df.format(num));
        //赋值
        entity.setNum(df.format(num));
    }

    private void add(TextView tvNum, FactoryParticipationEntity entity) {
        double num = Double.parseDouble(tvNum.getText().toString());
        num++;
        DecimalFormat df = new DecimalFormat("#####0.00");
        tvNum.setText(df.format(num));
        //赋值
        entity.setNum(df.format(num));
    }

    private void num(TextView tvNum, FactoryParticipationEntity entity) {
        editNumPopWindow(tvNum, entity);
    }

    private void selectTime(TextView tvTime, FactoryParticipationEntity entity) {
        dayCalendar.setTime(new Date(Long.valueOf(entity.getEndTime())));
        dayCalendar.add(Calendar.DAY_OF_MONTH, 1);
        TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            try {
                tvTime.setText(format);
                selectedDate.setTime(date);
                entity.setPickTime(String.valueOf(TimeUtil.getLongTime(format, "yyyy-MM-dd")));//时间存入实体类
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setTitleText("请选择时间")
                .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD"))
                .setRangDate(dayCalendar, null)
//                .setRangDate(null, null)//不限起始和结束时间
                .build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }

    //输入数量弹窗
    private void editNumPopWindow(TextView tvNum, FactoryParticipationEntity entity) {
        new EditJcNumPopWindow(context).builder().setTvNum(null == entity.getNum() ? " " : entity.getNum(),entity.getUnitName()).setEditNumCallBack(new EditJcNumPopWindow.EditNumCallBack() {
            @Override
            public void editNum(String num) {
                tvNum.setText(num);
                entity.setNum(String.valueOf(num));
            }
        }).show();
    }

}
